package com.koron.order.risk.keyPoint.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.risk.base.bean.convertor.LineRiskConvertor;
import com.koron.order.risk.keyPoint.bean.dto.LineRiskKeyPointDTO;
import com.koron.order.risk.keyPoint.bean.po.LineRiskKeyPoint;
import com.koron.order.risk.keyPoint.bean.query.LineRiskKeyPointQuery;
import com.koron.order.risk.keyPoint.bean.vo.LineRiskKeyPointExcel;
import com.koron.order.risk.keyPoint.bean.vo.LineRiskKeyPointVO;
import com.koron.order.risk.keyPoint.service.LineRiskKeyPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jzp
 * @since 2022/12/30
 */
@Api(tags = "风险关键点台账接口")
@RestController
@RequestMapping("lineRiskKeyPoint")
public class LineRiskKeyPointController {

    @Autowired
    private LineRiskKeyPointService lineRiskKeyPointService;

    @Autowired
    private LineRiskConvertor lineRiskConvertor;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<LineRiskKeyPointDTO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineRiskKeyPointService, "getDtoById", LineRiskKeyPointDTO.class, id));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<PageInfo<LineRiskKeyPointVO>> page(PageQuery pageQuery, LineRiskKeyPointQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), lineRiskKeyPointService, "page", PageInfo.class, pageQuery, query));
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    public Response<String> save(@RequestBody LineRiskKeyPoint lineRiskKeyPoint) {
        ADOConnection.runTask(getCurrDs(), lineRiskKeyPointService, "save", Boolean.class, lineRiskKeyPoint);
        return Response.ok(lineRiskKeyPoint.getId());
    }

    @SneakyThrows
    @ApiOperation(value = "导出", notes = "根据条件导出查询出的所有数据")
    @GetMapping("export")
    public void exportFile(HttpServletResponse response, LineRiskKeyPointQuery query) {
        String path = "excel/riskPoint_export.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("关键点台账", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<LineRiskKeyPointVO> list = ADOConnection.runTask(getCurrDs(), lineRiskKeyPointService, "list", List.class, query);
        List<LineRiskKeyPointExcel> collect = list.stream().map(e -> {
            LineRiskKeyPointExcel lineRiskKeyPointExcel = lineRiskConvertor.pointToExcel(e);
            String lineRiskPartStr = e.getLineRiskPartStr();
            lineRiskKeyPointExcel.setLineRiskPartStr(lineRiskPartStr);
            return lineRiskKeyPointExcel;
        }).collect(Collectors.toList());
        EasyExcel.write(response.getOutputStream())
                .withTemplate(classPathResource.getInputStream())
                .sheet(0)
                .needHead(false)
                .head(LineRiskKeyPointExcel.class)
                .doWrite(collect);
    }
}
